#!/bin/bash

#
# clandestine.googlecode.com
#



function Interpolate () {
	(( $# )) || return 0
	declare -i  i=0  LEN=${#1}  SPECIAL=0
	declare --  RESULT=""  CHAR=""  SUFFIX=""
	declare -r  S_CHAR="%"
	for (( i = 0; i < LEN; i += 1 )); do
		CHAR=${1:$i:1}
		if (( SPECIAL )); then
			if [[ "$S_CHAR" == "$CHAR" ]]; then
				SUFFIX="$S_CHAR"
			else
				if [[ "isset" == "${!CHAR+isset}" ]]; then
					SUFFIX="${!CHAR}"
				else
					SUFFIX="$CHAR"
				fi
			fi
			RESULT+="$SUFFIX"
			SPECIAL=0
		else
			if [[ "$S_CHAR" == "$CHAR" ]]; then
				SPECIAL=1
			else
				RESULT+="$CHAR"
			fi
		fi
	done
	(( SPECIAL )) && RESULT+="$S_CHAR"
	echo "$RESULT"
}
